package com.net.xpay.web.controller.merchant.collect;


import com.net.xpay.core.model.CollectionSafePayOrderSearchModel;
import com.net.xpay.core.model.CollectionSafePayOrderTransModel;
import com.net.xpay.core.model.IdModel;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.core.domain.collect.CollectionSafePayOrder;
import com.net.xpay.core.domain.safePay.SafePayBatch;
import com.net.xpay.core.manager.collect.CollectionSafePayOrderManager;
import com.net.xpay.core.service.safePay.CollectionSafePayService;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/merchant/collection-safe-pay-order")
@Api(tags = "商户/收集收款信息", description = "商户 收集收款信息 net")
public class CollectionSafePayOrderController {

    @Autowired
    private CollectionSafePayOrderManager collectionSafePayOrderManager;

    @Autowired
    private CollectionSafePayService collectionSafePayService;


    /**
     * 分页搜索
     */
    @ApiOperation("分页搜索")
    @PostMapping("/search")
    public ResultPagedList<CollectionSafePayOrder> searchCollectionSafePayOrder(@ApiIgnore @RequestUser User merchant, @RequestBody CollectionSafePayOrderSearchModel searchModel) {
        searchModel.setUserId(merchant.getId());
        List<CollectionSafePayOrder> records = collectionSafePayOrderManager.search(searchModel);
        long count = collectionSafePayOrderManager.searchCount(searchModel);
        return new ResultPagedList<>(records, count, searchModel);
    }


    /**
     * 发起转账
     */
    @ApiOperation("发起转账")
    @PostMapping("/trans")
    public ResultData<Long> trans(@ApiIgnore @RequestUser User merchant, @RequestBody CollectionSafePayOrderTransModel transModel) {
        log.info("trans collectionSafePayOrders transModel={}|userId={}", SerializationUtil.obj2String(transModel), merchant.getId());

        SafePayBatch transBatch = collectionSafePayService.createTransBatch(merchant, transModel);
        return new ResultData<>(transBatch.getId());
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
    @PostMapping("/delete")
    public ResultData<Void> deleteCollectionSafePayOrderById(@ApiIgnore @RequestUser User merchant, @RequestBody IdModel idModel) {
        log.info("deleteCollectionSafePayOrderById id={}|userId={}", idModel.getId(), merchant.getId());

        collectionSafePayService.deleteOrderById(merchant, idModel.getId());
        return new ResultData<>();
    }

}
